package com.example.minzhengtong.utils;

/**
 * created by WY on 2021/5/31.
 */

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.minzhengtong.MyApp;
import com.example.minzhengtong.R;


/**
 * 水印
 * <pre>
 *     author  : Fantasy
 *     version : 1.0, 2019-07-29
 *     since   : 1.0, 2019-07-29
 * </pre>
 */
public class Watermark {
    /**
     * 水印文本
     */
    private String mText;
    /**
     * 字体颜色，十六进制形式，例如：0xAEAEAEAE
     */
    private int mTextColor;
    /**
     * 字体大小，单位为sp
     */
    private float mTextSize;
    /**
     * 旋转角度
     */
    private float mRotation;
    private static Watermark sInstance;

    private Watermark() {
        mText = "";
        mTextColor = 0xAEAEAEAE;
        mTextSize = 18;
        mRotation = -33;
    }

    public static Watermark getInstance() {
        if (sInstance == null) {
            synchronized (Watermark.class) {
                sInstance = new Watermark();
            }
        }
        return sInstance;
    }

    /**
     * 设置水印文本
     *
     * @param text 文本
     * @return Watermark实例
     */
    public Watermark setText(String text) {
        mText = text;
        return sInstance;
    }

    /**
     * 设置字体颜色
     *
     * @param color 颜色，十六进制形式，例如：0xAEAEAEAE
     * @return Watermark实例
     */
    public Watermark setTextColor(int color) {
        mTextColor = color;
        return sInstance;
    }

    /**
     * 设置字体大小
     *
     * @param size 大小，单位为sp
     * @return Watermark实例
     */
    public Watermark setTextSize(float size) {
        mTextSize = size;
        return sInstance;
    }

    /**
     * 设置旋转角度
     *
     * @param degrees 度数
     * @return Watermark实例
     */
    public Watermark setRotation(float degrees) {
        mRotation = degrees;
        return sInstance;
    }

    /**
     * 显示水印，铺满整个页面
     *
     * @param activity 活动
     */
    public void show(Activity activity) {
        show(activity, mText);
    }

    /**
     * 显示水印，铺满整个页面
     *
     * @param activity 活动
     * @param text     水印
     */
    public void show(Activity activity, String text) {
        WatermarkDrawable drawable = new WatermarkDrawable();
        drawable.mText = text;
        drawable.mTextColor = mTextColor;
        drawable.mTextSize = mTextSize;
        drawable.mRotation = mRotation;

        ViewGroup rootView = activity.findViewById(android.R.id.content);
        FrameLayout layout = new FrameLayout(activity);
        layout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setBackground(drawable);
        rootView.addView(layout);
    }

    /**
     * 显示水印，在某个VIEW里显示
     *
     * @param activity 活动
     * @param text     水印
     */
    public void show(Activity activity, String text,ViewGroup rootView) {
        WatermarkDrawable drawable = new WatermarkDrawable();
        drawable.mText = text;
        drawable.mTextColor = mTextColor;
        drawable.mTextSize = mTextSize;
        drawable.mRotation = mRotation;

        FrameLayout layout = new FrameLayout(activity);
        layout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setBackground(drawable);
        rootView.addView(layout);
    }

    private class WatermarkDrawable extends Drawable {
        private Paint mPaint;
        /**
         * 水印文本
         */
        private String mText;
        /**
         * 字体颜色，十六进制形式，例如：0xAEAEAEAE
         */
        private int mTextColor;
        /**
         * 字体大小，单位为sp
         */
        private float mTextSize;
        /**
         * 旋转角度
         */
        private float mRotation;

        private WatermarkDrawable() {
            mPaint = new Paint();
        }

        @Override
        public void draw(@NonNull Canvas canvas) {
            int width = getBounds().right;
            int height = getBounds().bottom;
            int diagonal = (int) Math.sqrt(width * width + height * height); // 对角线的长度


//            mPaint.setColor(mTextColor);
            mPaint.setColor(MyApp.getContext().getResources().getColor(R.color.textColor111));
            mPaint.setTextSize(WUtils.spToPx(MyApp.getContext(),14f)); // ConvertUtils.spToPx()这个方法是将sp转换成px，ConvertUtils这个工具类在我提供的demo里面有
            mPaint.setAntiAlias(true);
            mPaint.setAlpha(30);
            float textWidth = mPaint.measureText(mText);

            canvas.drawColor(0x00000000);
            //canvas.rotate(mRotation);

//            int index = 0;
//            float fromX;
//            // 以对角线的长度来做高度，这样可以保证竖屏和横屏整个屏幕都能布满水印
//            for (int positionY = diagonal / 6; positionY <= diagonal; positionY += diagonal / 6) {
//                fromX = -width + (index++ % 2) * textWidth; // 上下两行的X轴起始点不一样，错开显示
//                for (float positionX = fromX; positionX < width; positionX += textWidth * 6) {
//                    canvas.drawText(mText, positionX, positionY, mPaint);
//                }
//            }

/*            int xSize = 4;
            int ySize =8;

            int index = 0;
            float fromX;
            // 以对角线的长度来做高度，这样可以保证竖屏和横屏整个屏幕都能布满水印
            for (int positionY = diagonal / ySize; positionY <= diagonal; positionY += diagonal / ySize) {
                fromX = -width + (index++ % xSize) * textWidth; // 上下两行的X轴起始点不一样，错开显示
                fromX=0;
                for (float positionX = fromX; positionX < width; positionX += textWidth * ySize) {
                    canvas.save();
                    canvas.rotate(mRotation, positionX, positionY);
                    canvas.drawText(mText, positionX, positionY, mPaint);
                    //canvas.save();
                    canvas.restore();

                }
            }*/

            double xCell = width/4;
            double yCell = height/4.7;
            for (int i=0;i<2;i++){
                for (int j=0;j<5;j++){
                    double x = xCell*(i+1);
                    double y = yCell*(j+1);
                    if(i==0){
                        x = width*1/6;
//                        y = yCell*(j+0.5);
                    }else{
                        x = width*5.6/8;
//                        y = yCell*(j+0.5);
                    }
                    if (j == 4){
                        y = yCell*(j+0.18);
                    }else {
                        y = yCell*(j+0.3);
                    }
                    canvas.save();
                    canvas.rotate(mRotation,(int) x,(int) y);
                    canvas.drawText(mText,(int) x, (int)y, mPaint);
                    //canvas.save();
                    canvas.restore();
                }
            }

/*            canvas.save();
            canvas.restore();*/
        }

        @Override
        public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        }

        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

    }
}